package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : RegisterActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月9日 下午10:02:08
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {注册用户}
 *
 */
@EActivity(R.layout.register)
public class RegisterActivity extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	Button register_butt;
	@ViewById
	EditText passwd_edit;
	@ViewById
	EditText repasswd_edit;

	@AfterViews
	public void initView() {
		topBar.setTopBarClickListener(new TopBarClickListener() {

			@Override
			public void rightBtnClick() {

			}

			@Override
			public void leftBtnClick() {
				onBackPressed();
			}
		});
	}

	@Click({ R.id.register_butt})
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register_butt:
			String passwd=passwd_edit.getText().toString().trim();
			String repasswd=repasswd_edit.getText().toString().trim();
			if(TextUtils.isEmpty(passwd) |TextUtils.isEmpty(repasswd)){
				ToastUtils.show(context, "密码不能为空!");
				return;
			}else if(!passwd.equals(repasswd)){
				ToastUtils.show(context, "两次密码不一致!");
				return;
			}
			
			
			
			//发送注册请求
			RequestParams params=new RequestParams();
			 params.put("username",myprefs.phone().get());
             params.put("password", passwd);
             params.put("token", myprefs.device_token().get());
			 params.put("channel_id", "xiaomi");
			 params.put("type", "1");
			AsyncHttpUtil.post(URLConfigs.Register, params, RegisterResponseHandler);
			break;

		default:
			break;
		}
	}
	
	
	
	//请求响应监听
	TextHttpResponseHandler RegisterResponseHandler =new TextHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] arg1, String result) {
			if(statusCode==200){
				LogUtil.e(TAG, "RegisterActivity json :"+result);
//				GsonUtil.GetFromJson(json, t)
				
				//跳转
				openActivity(LoginActivity_.class);
				RegisterActivity.this.finish();
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
			ToastUtils.show(context, R.string.login_fail);
		}
	};
	
}
